package com.test.reflect;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
 * java反射机制 Map转Bean
 * @author chris.wei
 *
 */
public class ReflectMapToBean {
	public static void main(String[] args) throws Exception {
		Book book = new Book();
		book.setId(22);
		book.setName("Java编程思想22222222");
		book.setPublishName("人民大学出版社2222222222");
		book.setCreateTime(new Date().getTime());
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", 1);
		map.put("name", "Java编程思想");
		map.put("publishName", "人民大学出版社");
		map.put("createTime", new Date().getTime());
		Iterator<Entry<String, Object>> it = map.entrySet().iterator();
		while(it.hasNext()){
			Entry<String, Object> ent = it.next();
			System.out.println(ent.getKey() + ": " + ent.getValue() + ", " + ent.getValue().getClass());
			PropertyDescriptor pd = new PropertyDescriptor(ent.getKey(), book.getClass());
			Method set = pd.getWriteMethod();
			set.invoke(book, ent.getValue());
		}
		System.out.println(book.getId() + ", " + book.getName() + ", " + book.getPublishName());
	}
}
